<template>
     <div class="dashboard-container">
    <div class="app-container">
        <!-- <rain :rainNumber='20' :rotateDeg="40" :w='1' :h='140' style="z-index: 99;"/> -->
        <el-card shadow="never">
            <!-- <rain :rainNumber='20' :rotateDeg="40" :w='1' :h='140'/> -->
            <el-breadcrumb separator-class="el-icon-arrow-right" v-if="dID.id">
                <el-breadcrumb-item>学科</el-breadcrumb-item>
                <el-breadcrumb-item>{{this.dID.name}}</el-breadcrumb-item>
                <el-breadcrumb-item>目录</el-breadcrumb-item>
            </el-breadcrumb>
            <el-row type="flex" justify="space-between">
                <el-col>
                    <el-form :inline="true" :model="formInline">
                    <!-- 目录 -->
                    <el-form-item label="目录名称">
                        <el-input v-model="formInline.directoryName" style="width: 200px;" size="small"/>
                    </el-form-item>
                    <!-- 状态 -->
                    <el-form-item label="状态" style="margin-left: 20px">
                        <el-select v-model="formInline.state" size="small" style="width: 200px;" placeholder="请选择">
                            <el-option label="已禁用" value="0" />
                            <el-option label="已启用" value="1" />
                        </el-select>
                    </el-form-item>
                    <!-- 清除/搜索 -->
                    <el-form-item>
                        <el-button size="small" @click="clear">清除</el-button>
                        <el-button size="small" type="primary" @click="search">搜索</el-button>
                    </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="2">
                    <el-button v-if="dID.id" type="text" icon="el-icon-back" @click="$router.back()" class="cancel">返回学科</el-button>
                </el-col>
                <el-col :span="2">
                    <el-button size="small" type="success" @click="addDire"><i class="el-icon-edit"></i>新增目录</el-button>
                </el-col>
            </el-row>
            <el-alert title="" type="info" :closable="false" show-icon>
                <span slot='title'>一共有{{total}}条数据</span>
            </el-alert>
            <!-- 表格 -->
            <el-table :data='directoryList' style="width: 100%">
                <el-table-column size="small" label="序号" type="index"/>
                <el-table-column property="subjectName" size="small" label="所属学科"/>
                <el-table-column property="directoryName" size="small" label="目录名称"/>
                <el-table-column property="username" size="small" label="创建者"/>
                <el-table-column property="addDate" size="small" label="创建日期" >
                    <template #default="{row}">
                        {{row.addDate | parseTimeByString }}
                    </template>
                </el-table-column>
                <el-table-column property="totals" label="面试题数量数量"/>
                <el-table-column property="state" label="状态">
                    <template #default="{row}">
                        <span v-if='+row.state===1'>已启用</span>
                        <span v-else>已禁用</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="240px">
                    <template #default="{row}">
                        <el-button type="text" @click="Disabled(row.id,row.state)" size="small">
                            <span v-if="row.state" size="small">禁用</span>
                            <span v-else size="small">开启</span>
                        </el-button>
                        <el-button type="text" size="small" :disabled="row.state==1?true:false" @click="editDire(row)">修改</el-button>
                        <el-button type="text" size="small" :disabled="row.state==1?true:false" @click="delDire(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" align="middle">
                <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="directoryObj.page" :page-sizes="[5, 10, 20,50]" :page-size="directoryObj.pagesize" layout="prev, pager, next,sizes, jumper" :total="total">
                </el-pagination>
            </el-row>
        </el-card>

        <DirectorysAdd @update='loadList' ref="DirectorysAddRef" :showDialog.sync="showDialog" />
  </div>
  </div>
</template>

<script>

import { list, remove, changeState, detail } from '@/api/hmmm/directorys'
import DirectorysAdd from '../components/directorys-add.vue'

export default {
  name: 'DirectorysPage',
  components: {
    DirectorysAdd
  },
  data () {
    return {
      directoryList: [],
      directoryObj: {
        page: 1,
        pagesize: 10
      },
      formInline: {
        directoryName: '',
        state: null
      },
      total: 0,
      showDialog: false,
      dID: this.$route.query,
      formData: {
        directoryName: '',
        id: '',
        subjectID: ''
      }
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    async loadList () {
      const res = await list({ ...this.directoryObj, subjectID: this.dID.id })
      this.directoryList = res.data.items
      this.total = res.data.counts
    },
    handleSizeChange (size) {
      this.directoryObj.pagesize = size
      this.loadList()
    },
    handleCurrentChange (page) {
      this.directoryObj.page = page
      this.loadList()
    },
    async delDire (row) {
      try {
        await this.$confirm('此操作将永久删除该目录, 是否继续?')
        await remove(row)
        this.$message.success('删除成功')
        const totalPage = Math.ceil((this.total - 1) / this.directoryObj.pagesize)
        this.directoryObj.page = this.directoryObj.page > totalPage ? totalPage : this.directoryObj.page
        this.directoryObj.page = this.directoryObj.page < 1 ? 1 : this.directoryObj.page
        this.loadList()
      } catch (error) {
        console.log(error)
      }
    },
    addDire () {
      this.showDialog = true
    },
    async editDire (row) {
      this.$refs.DirectorysAddRef.getDetail(row)
      this.formData = await detail(row)
      this.showDialog = true
    },
    async clear () {
      // 清除目录名称、状态
      this.formInline = {}
      // 刷新页面
      await this.loadList()
    },
    async search () {
      if (this.formInline.directoryName || this.formInline.state) {
        this.directoryObj.page = 1
        const res = await list({ ...this.directoryObj, state: this.formInline.state, directoryName: this.formInline.directoryName })
        this.directoryList = res.data.items
        this.total = res.data.counts
        if (this.total > 0) {
          this.$message.success('查询成功')
        } else {
          this.$message.error('查询失败,请重新查询')
        }
      } else {
        this.$message.warning('请输入内容')
      }
    },
    async Disabled (id, state) {
      try {
        state = state ? 0 : 1
        this.directoryList.forEach(item => {
          if (item.id === id) {
            item.state = state
          }
        })
        await changeState(id, state)
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.container{
    padding: 20px;
}
.el-card{
  .el-icon-edit{
    margin-right: 10px;
  }
  .inputClass{
    margin: 0 10px;
  }
  .el-alert{
    margin:0 0 20px 0;
  }
  .text{
    margin-left: 40px;
  }
  .el-breadcrumb{
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e8e8e8;
  }
}
</style>
